package com.ska.skautils.utilcode.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;



/**
 * author:      Fubiven
 * data:        2019/4/13
 * version:     1.0
 * describe:    SharedPreferences工具类<br/>
 * <b>注意:</b> key值查看SPConstant常量管理类
 */
public class SPUtils {
    private SharedPreferences mSharedPreferences;
    public static final String SP_FILE_NAME = "share_data";

    private SPUtils() {
        mSharedPreferences = SkaUtils.getApp().getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
    }

    private static class SPUtilsHolder {
        private static final SPUtils mInstance = new SPUtils();
    }

    public static void putInt(String key, int value) {
        SPUtilsHolder.mInstance.mSharedPreferences.edit().putInt(key, value).apply();
    }

    public static int getInt(String key, int dValue) {
        return SPUtilsHolder.mInstance.mSharedPreferences.getInt(key, dValue);
    }

    public static void putBoolean(String key, boolean value) {
        SPUtilsHolder.mInstance.mSharedPreferences.edit().putBoolean(key, value).apply();
    }

    public static boolean getBoolean(String key, boolean dValue) {
        return SPUtilsHolder.mInstance.mSharedPreferences.getBoolean(key, dValue);
    }

    public static void putLong(String key, long value) {
        SPUtilsHolder.mInstance.mSharedPreferences.edit().putLong(key, value).apply();
    }

    public static long getLong(String key, long dValue) {
        return SPUtilsHolder.mInstance.mSharedPreferences.getLong(key, dValue);
    }

    public static void putString(String key, String value) {
        SPUtilsHolder.mInstance.mSharedPreferences.edit().putString(key, value).apply();
    }

    public static String getString(String key, String dValue) {
        return SPUtilsHolder.mInstance.mSharedPreferences.getString(key, dValue);
    }

    public static boolean isExist(String key) {
        return SPUtilsHolder.mInstance.mSharedPreferences.contains(key);
    }

    public static boolean isExistValue(String key) {
        return !TextUtils.isEmpty(getString(key, ""));
    }

    public static void remove(String key) {
        if (isExist(key)) {
            SharedPreferences.Editor editor = SPUtilsHolder.mInstance.mSharedPreferences.edit();
            editor.remove(key);
            editor.apply();
        }
    }

    public static void clearAllData() {
        SharedPreferences.Editor editor = SPUtilsHolder.mInstance.mSharedPreferences.edit();
        editor.clear();
        editor.commit();
    }
}
